package com.light.gulimall.seckill.controller;

import com.light.common.utils.R;
import com.light.gulimall.seckill.service.SecKillSkuService;
import com.light.gulimall.seckill.to.SecKillSkuRedisTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class SecKillSkuController {

    @Autowired
    private SecKillSkuService secKillSkuService;

    /**
     * 返回当前时间可以参与的秒杀信息
     * @return
     */
    @GetMapping("/currentSeckillSkus")
    public R getCurrentSeckillSkus(){
        List<SecKillSkuRedisTO> secKillSkuRedisTOS =  secKillSkuService.getCurrentSeckillSkus();
        return R.ok().put("data",secKillSkuRedisTOS);
    }

    @GetMapping("/currentSeckillSkus/{skuId}")
    public SecKillSkuRedisTO getCurrentSeckillSkuById(@PathVariable("skuId") Long skuId){
        return secKillSkuService.getCurrentSeckillSkuById(skuId);
    }

    /**
     * 秒杀方法
     * @param killId    场次id_skuId
     * @param key   随机码
     * @param num   数量
     * @return  是否秒杀成功
     */
    @GetMapping("/kill")
    public R kill(@RequestParam("killId")String killId,
                  @RequestParam("key")String key,
                  @RequestParam("num")Integer num){
        String orderNo = secKillSkuService.kill(killId,key,num);
        return R.ok().put("result",orderNo);
    }
}
